<template>
  <div>
    <table>
      <!-- 2. 使用tr组件, 传入需要的数据 -->
      <MyTr
        v-for="(item, index) in goodsArr"
        :key="index"
        :obj="item"
        :index="index"
        @change-count-event="changeCount"
      ></MyTr>
    </table>
    <p>All Number:{{ sumNumber }}</p>
  </div>
</template>

<script>
import MyTr from "../components/practice-list.vue";
export default {
  data() {
    return {
      goodsArr: [
        {
          count: 0,
          goodsName: "Watermelon",
        },
        {
          count: 0,
          goodsName: "Banana",
        },
        {
          count: 0,
          goodsName: "Orange",
        },
        {
          count: 0,
          goodsName: "Pineapple",
        },
        {
          count: 0,
          goodsName: "Strawberry",
        },
      ],
    };
  },
  components: {
    MyTr,
  },
  computed: {
    sumNumber() {
      return this.goodsArr.reduce((sum, obj) => (sum += obj.count * 1), 0);
    },
  },
  methods: {
    changeCount(index, value) {
      this.goodsArr[index].count = value;
    },
  },
};
</script>

<style scoped>
table {
  margin: 0 auto;
}
</style>
